<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
      <el-form-item label="用户类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择用户类型" clearable @keyup.enter.native="handleQuery">
          <el-option v-for="item in options" :key="item" :label="item" :value="item" />
        </el-select>
      </el-form-item>
      <el-form-item label="用户账号" prop="username">
        <el-input v-model="queryParams.username" placeholder="请输入用户账号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="用户昵称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入用户昵称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" v-hasPermi="['monitor:online-user:list']" icon="el-icon-search" size="small"
          @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
        <el-button type="danger" v-hasPermi="['monitor:online-user:force']" :disabled="multiple" plain size="small"
          icon="el-icon-delete" @click="handleForceLogoutBatch">批量强退</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="list.slice((currentPage-1)*pageSize,currentPage*pageSize)"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="45" align="center" />
      <el-table-column label="用户类型" align="center" prop="type" :show-overflow-tooltip="true" />
      <el-table-column label="用户账号" align="center" prop="username" />
      <el-table-column label="用户昵称" align="center" prop="name" :show-overflow-tooltip="true" />
      <el-table-column label="所属院系" align="center" prop="collegeName" :show-overflow-tooltip="true" />
      <el-table-column label="所属岗位" align="center" prop="postName" />
      <el-table-column label="在线时间" align="center" prop="onlineTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-hasPermi="['monitor:online-user:force']" size="mini" type="text" icon="el-icon-delete"
            @click="handleForceLogout(scope.row)">强退</el-button>
        </template>
      </el-table-column>
    </el-table>

    <Pagination v-show="total>0" :total="total" :page.sync="currentPage" :limit.sync="pageSize" />
  </div>
</template>

<script>
  import Pagination from '@/components/Pagination'
  import {
    listOnlineUser,
    forceUser
  } from "@/api/monitor/online-user";

  export default {
    components: {
      Pagination
    },
    name: "OnlineUser",
    data() {
      return {
        loading: true,
        // 总条数
        total: 0,
        // 表格数据
        list: [],
        multiple: true,
        ids: [],
        currentPage: 1,
        pageSize: 10,
        queryParams: {
          type: '',
          username: '',
          name: ''
        },
        options: ['管理员', '企业', '学生']
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询在线用户列表 */
      getList() {
        this.loading = true,
          listOnlineUser(this.queryParams).then(response => {
            this.list = response.data;
            this.total = response.data.length;
            this.loading = false
          });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.currentPage = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.queryParams = {}
        this.handleQuery();
      },
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.multiple = !selection.length
      },
      /** 强退按钮操作 */
      handleForceLogout(row) {
        let ids = row.id || this.ids
        this.$confirm(`确定要强退id为【${ids}】的用户`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          forceUser(ids).then(res => {
            this.getList();
            this.$message.success("强退成功");
          })
        }).catch(() => {});
      },
    }
  };
</script>
